package com.design3.graduatethesissys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.design3.graduatethesissys.common.utils.FileUtil;
import com.design3.graduatethesissys.common.utils.GetUserName;
import com.design3.graduatethesissys.common.utils.ResetPasswordUtils;
import com.design3.graduatethesissys.common.utils.UUIDUtils;
import com.design3.graduatethesissys.domain.PingJia;
import com.design3.graduatethesissys.domain.Subject;
import com.design3.graduatethesissys.domain.Teacher;
import com.design3.graduatethesissys.service.ChuqiService;
import com.design3.graduatethesissys.service.SubjectService;
import com.design3.graduatethesissys.service.TeacherService;

/**
 * @author 作者 罗穆健:
 * @version 创建时间：2020年1月5日 下午10:53:10 类说明
 */
@Controller
@RequestMapping(value = "Teacher")
public class TeacherController {

	@Autowired
	SubjectService subjectService;

	@Autowired
	TeacherService teacherService;
	
	@Autowired
	ChuqiService chuqiService;

	@RequestMapping(value = "")
	public String index() {
		return "Teacher/test";
	}

	/**
	 * 教师出题
	 * 
	 * @param subject
	 * @return 成功或失败信息
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public Map<String, String> teacherSetTopic(Subject subject, MultipartFile file) {
		subject.setSbj_id(UUIDUtils.getUUID());
		subject.setTeh_id(GetUserName.getUserName());
		Map<String, String> result1 = new HashMap<>();
		if(file != null) {
			result1 = teacherService.uploadSubject(file, subject.getSbj_id());
		}
		Map<String, String> result2 = subjectService.subjectSetTopic(subject);
		result2.putAll(result1);
		return result2;
	}
	
	@ResponseBody
	@RequestMapping(value = "/selectallsbj")
	public List<Map<String, Object>> teacherSetTopic(){
		String teh_id = GetUserName.getUserName();
		return teacherService.selectAllSbj(teh_id);
	}

	/**
	 * 查询所有教师
	 * 
	 * @return 所有教师及相关信息
	 */
	@ResponseBody
	@RequestMapping(value = "/select")
	public List<Map<String, Object>> selectTeacher() {
		return teacherService.selectTeacher();
	}

	/**
	 * 更新教师个人信息
	 * 
	 * @param teacher
	 * @return 成功或失败信息
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Map<String, Object> updateTeacher(Teacher teacher) {
		return teacherService.updateTeacher(teacher);
	}
	
	/**
	 * 修改密码接口
	 * @param oldPassword 旧密码
	 * @param newPassword 新密码
	 * @param confirmPassword 新密码确认
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modifypwd")
	public Map<String, String> modifyPwd(String oldPassword,String newPassword,String confirmPassword){
		String result = ResetPasswordUtils.resetPassword(oldPassword, newPassword, confirmPassword);
		Map<String, String> map = new HashMap<>();
		map.put("message", result);
		return map;
	}
	
	/**
	 * 更新暂时保存的毕设题目
	 * 
	 * @param subject
	 *            状态位为ct01时才能进行修改
	 * @return 成功或失败信息
	 */
	@ResponseBody
	@RequestMapping(value = "/updatesbj")
	public Map<String, String> updateSubject(Subject subject, MultipartFile file) {
		Map<String, String> result1 = new HashMap<>();
		if(file != null) {
			result1 = teacherService.uploadSubject(file, subject.getSbj_id());
		}
		Map<String, String> result2 = subjectService.subjectUpdate(subject);
		result2.putAll(result1);
		return result2;
	}

	/**
	 * 删除一个毕设题目
	 * 
	 * @param sbj_id
	 * @return 成功或失败信息
	 */
	@ResponseBody
	@RequestMapping(value = "/deletesbj")
	public Map<String, Object> deleteSubject(@RequestParam("sbj_id") String sbj_id) {
		return subjectService.subjectDelete(sbj_id);
	}

	/**
	 * 查看一个教师所负责的所有学生及相关信息
	 * 
	 * @param teh_id
	 * @return 所有学生及毕设相关信息
	 */
	@ResponseBody
	@RequestMapping(value = "/getcqstudents")
	public List<Map<String, Object>> getCqStudentInfo() {
		String teh_id = GetUserName.getUserName();
		return teacherService.selectCqStudent(teh_id);
	}
	@ResponseBody
	@RequestMapping(value = "/getzqstudents")
	public List<Map<String, Object>> getZqStudentInfo() {
		String teh_id = GetUserName.getUserName();
		return teacherService.selectZqStudent(teh_id);
	}
	@ResponseBody
	@RequestMapping(value = "/getzzstudents")
	public List<Map<String, Object>> getZzStudentInfo() {
		String teh_id = GetUserName.getUserName();
		return teacherService.selectZzStudent(teh_id);
	}
	/**
	 * 所有负责的学生，接口提供给聊天用
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getallstudents")
	public List<Map<String, Object>> getAllStudentInfo() {
		String teh_id = GetUserName.getUserName();
		System.out.println(teacherService.selectAllStudent(teh_id));
		return teacherService.selectAllStudent(teh_id);
	}
	
	/**
	 * 保存教师对学生初（中）期评价
	 * @param stu_id
	 * @return 成功或失败信息
	 */
	@ResponseBody
	@RequestMapping(value = "/savepj")
	public Map<String, Object> savePingjia(PingJia pingjia){
		return chuqiService.savePingjia(pingjia);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getpj")
	public List<PingJia> getPingjia(String stu_id, String pj_status){
		return teacherService.getPingjia(stu_id, pj_status);
	}
	
	/**
	 * 教师上传课题书接口（弃用）
	 * @param file
	 * @return
	 */
//	@ResponseBody
//	@RequestMapping(value = "/uploadsbj")
//	public Map<String, String> uploadSubject(MultipartFile file){
//		return teacherService.uploadSubject(file);
//	}
	
	/**
	 * 教师下载报告接口
	 * @param response
	 * @param stu_id
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/download")
	public String downloadWord(HttpServletResponse response, String sbj_id){
		String teh_id = GetUserName.getUserName();
		return FileUtil.download(response, teh_id, sbj_id);
	}
	
	/**
	 * 学生报告下载接口
	 * @param response
	 * @param sbj_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/downloadstu")
	public String downloadStuWord(HttpServletResponse response, String stu_id, String type){
		return FileUtil.download(response, stu_id, type);
	}
	
}
